package sv.com.seguros.dao.ctg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.seguros.dao.BaseDaoImpl;
import sv.com.seguros.dto.ctg.CtgCatalogo;
import sv.com.seguros.exception.WebException;
import sv.com.seguros.web.util.WebUtilidades;

public class CtgCatalogoDaoImpl extends BaseDaoImpl implements CtgCatalogoDao{

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllByCodigoAsArray(String ctgCatalogoPadre) throws WebException{
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgCatalogo.class);
		criteria.add(Restrictions.eq("ctgCatalogoPadre", ctgCatalogoPadre));
		criteria.addOrder(Order.asc("ctgCatalogoNombre"));
		criteria.setProjection(Projections.projectionList().
			add(Projections.property("ctgCatalogoId")).
			add(Projections.property("ctgCatalogoNombre")).
			add(Projections.property("ctgCatalogoPadre")).
			add(Projections.property("ctgCatalogoHijo")).
			add(Projections.property("ctgCatalogoActivo"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> findAllActivesByCodigoAsArray(String ctgCatalogoPadre) throws WebException{
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgCatalogo.class);
		criteria.add(Restrictions.eq("ctgCatalogoActivo", "1"));
		criteria.add(Restrictions.eq("ctgCatalogoPadre", ctgCatalogoPadre));
		criteria.addOrder(Order.asc("ctgCatalogoNombre"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgCatalogoId")).
				add(Projections.property("ctgCatalogoNombre")).
				add(Projections.property("ctgCatalogoHijo"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}
	
	@SuppressWarnings("unchecked")
	public CtgCatalogo findByCodigo(String ctgCatalogoHijo) throws WebException{
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(CtgCatalogo.class);
			criteria.add(Restrictions.eq("ctgCatalogoHijo", ctgCatalogoHijo));
			List<CtgCatalogo> list;
			list = (List<CtgCatalogo>) findByCriteria(criteria);
			if(!WebUtilidades.isEmptyList(list)) return list.get(0);
			return null;
		} catch (WebException e) {
			throw e;
		} catch (Exception e) {
			throw new WebException("Problemas a extraer la informaci\u00F3n del cat\u00E1logo");
		}
	}
	
	public void delete(Long ctgCatalogoId) throws WebException{
		try {
			String hqlQuery = "delete from CtgCatalogo where ctgCatalogoId = :ctgCatalogoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgCatalogoId", ctgCatalogoId);
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (Exception e) {
			throw new WebException("Problemas al tratar de eliminar el registro <br />Registro puede estar asociado a un Cliente, Poliza, etc");
		}
	}
	
	public void toggleActive(Long ctgCatalogoId, String ctgCatalogoActivo) throws WebException{
		try {
			String hqlQuery = "update CtgCatalogo set ctgCatalogoActivo = :ctgCatalogoActivo where ctgCatalogoId = :ctgCatalogoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgCatalogoId", ctgCatalogoId);
			parameters.put("ctgCatalogoActivo", "1".equals(ctgCatalogoActivo) ? "0" : "1");
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (WebException e) {
			throw e;
		} catch (Exception e) {
			throw new WebException("Problemas al actualizar la informaci\u00F3n");
		}
	}
}
